home *** CD-ROM | disk | FTP | other *** search
-
- /* this deals with the triggering of moves by user
- or (by alarm) for computer; the queueing of data for
- later display; and the updating of the window and sound
- (also by alarm) */
-
- #include "kalaha.h"
-
-
-
- static void translate_real_to_working(char * h);
- static void update_display(int p, int gain);
- static int display_move(char * h, int p);
- static void queue_a_plop(int p, int gain);
- static void comp_moves(void);
- static void real_move(int p);
-
- /* control passes to functions in this file via the
- following two extern functins */
-
-
- void icon_hit(wimp_i i)
- {
- int layer;
-
- /* do nothing if move in progress */
- if ( plopsdone < nofplops )
- return;
-
- /* click on arrow */
- if (i==6 && !automove && starterplayer != UservUser)
- {
- comp_moves();
- return;
- }
-
- layer = which_layer(i);
- if (layer != TOPLAY && layer != BOTLAY)
- return;
- /* Now left with clicks on move icons. Eliminate illegal moves */
- if (layer == TOPLAY && (botgo || top[i-7]==0 || starterplayer != UservUser))
- return;
- if (layer == BOTLAY && ((!botgo) || bot[i]==0))
- return;
-
- real_move(i);
- }
-
-
-
-
- void alarm_handler_proc(int calledat, void *handle)
- {
- handle = handle;
-
- if (plopsdone == nofplops) /* if no move in progress */
- {
- plopsdone = nofplops = 0;
- if (automove && starterplayer != UservUser)
- comp_moves();
- }
-
- if (plopsdone < nofplops)
- {
- update_display(pendingplops[plopsdone].p, pendingplops[plopsdone].gain);
- plopsdone += 1;
- }
-
- while (calledat <= alarm_timenow())
- calledat += 50;
- alarm_set(calledat, alarm_handler_proc, (void *)0 );
- }
-
-
-
- void queue_a_plop(int p, int gain)
- {
- if (p<14 && gain == 0)
- return;
- pendingplops[nofplops].p = p;
- pendingplops[nofplops].gain = gain;
- nofplops += 1;
- }
-
-
-
- void comp_moves(void)
- {
- char here[14];
- int move, depth;
-
- depth = 5*level/3;
- wassert(depth < MAXDEPTH);
- if (!botgo && (top[0]|top[1]|top[2]|top[3]|top[4]|top[5]) && starterplayer != UservUser)
- {
- visdelay_begin();
- translate_real_to_working(here);
- if (level == 0)
- move = stupid_move(here);
- else
- move = best_n_move(depth, here).move;
- visdelay_end();
-
- real_move( 7 + move );
- }
- }
-
-
-
-
- void update_display(int p, int gain)
-
- /* p is 0...14, 0...13 being c-clockwise from mover's botleft
- gain is change to h[p], can be 0 or -ve
- p>=14 is a special case, meaning update botgo and arrow according to
- gain which is TRUE if change player, and deselect starting icon (p-14)
- always.
- */
- {
- wimp_redrawstr r;
- BOOL more;
- int balls;
-
-
- /* convert from mover's to bot's point of view */
- if (which_layer(p) != OTHERLAY && !botgo)
- {
- p += 7;
- if (p>13)
- p -= 14;
- }
-
- switch (which_layer(p))
- {
- case BOTLAY:
- bot[p] += gain;
- break;
- case TOPLAY:
- top[p-7] += gain;
- break;
- case MIDRIGHT:
- bottot += gain;
- break;
- case MIDLEFT:
- toptot += gain;
- break;
- case OTHERLAY:
- if (gain==TRUE)
- botgo = !botgo;
- break;
- }
-
- if (which_layer(p) != OTHERLAY && gain>=1 && soundon)
- {
- int oldbeats, oldtempo;
- /* attempt not to interfere with other programs' sound
- - probably doomed to failure anyway */
- oldbeats = bbc_getbeats();
- oldtempo = bbc_gettempo();
-
- wimpt_noerr(bbc_soundon());
- wimpt_noerr(bbc_settempo(4096));
- wimpt_noerr(bbc_setbeats(0));
- balls = 1;
- while (balls<gain)
- {
- wimpt_noerr(bbc_sound(1, -13, 103, 1, 5*balls));
- balls += 1;
- }
- wimpt_noerr(bbc_sound(1, -13, 103, 2, 5*gain));
- wimpt_noerr(bbc_sound(1, -13, 108, 1, 5*gain+7));
-
- wimpt_noerr(bbc_settempo(oldtempo));
- wimpt_noerr(bbc_setbeats(oldbeats));
- }
-
- r.w = main_window_handle;
- find_hollow_box(&(r.box), p);
-
- wimpt_noerr(wimp_update_wind( &r, &more ));
- while (more)
- {
- plot_one_hollow(p, r);
- wimpt_noerr(wimp_get_rectangle(&r, &more));
- }
-
-
- switch (which_layer(p))
- {
- case BOTLAY:
- write_iconnum(bot[p], p);
- wimpt_noerr(wimp_set_icon_state(main_window_handle, p, 0, 0));
- break;
-
- case TOPLAY:
- write_iconnum(top[p-7], p);
- wimpt_noerr(wimp_set_icon_state(main_window_handle, p, 0, 0));
- break;
-
- case MIDRIGHT:
- write_iconnum(bottot, 13);
- wimpt_noerr(wimp_set_icon_state(main_window_handle, 13, 0, 0));
- break;
-
- case MIDLEFT:
- write_iconnum(toptot, 14);
- wimpt_noerr(wimp_set_icon_state(main_window_handle, 14, 0, 0));
- break;
-
- case OTHERLAY:
- set_icon_select_state(main_window_handle, p-14, FALSE);
- break;
- }
- }
-
-
-
-
-
- int display_move(char * h, int p)
- /* do a displayed move */
- /*
- passed game situation in h[0...13]. posn p, 0<=p<5, is
- to move.
- 12 11 10 9 8 7
- 13 6
- 0 1 2 3 4 5
-
- If botgo, this maps directly to screen positions, else add 7 mod 14.
- */
- /*
- Returns: MY,YOUR,OVER according to another go, opp's go, game end
- Updates h[] to new values
- */
- /*
- this is very similar to virtual_move(), but calls queue_a_plop() to
- update the 'real' (global) variables bot[], top[], botot, and
- toptot (but not botgo) and then force a redraw.
- */
- {
- int carry, i, temp;
-
- carry = h[p];
- h[p] = 0; queue_a_plop(p, -carry);
-
- while (carry-->0)
- {
- p++;
- if (p==13) p=0;
- h[p] += 1; queue_a_plop(p, 1);
- }
- if (p < 6 && h[p] == 1)
- {
- h[p] = 0; queue_a_plop(p, -1);
- h[6] += 1; queue_a_plop(6, 1);
- temp = h[12-p];
- h[12-p] = 0; queue_a_plop(12-p, -temp);
- h[6] += temp; queue_a_plop(6, temp);
- }
- if (h[0]==0 && h[1]==0 && h[2]==0 && h[3]==0 && h[4]==0 && h[5]==0)
- {
- for (i=7; i<13; i++)
- {
- temp = h[i];
- h[i] = 0; queue_a_plop(i, -temp);
- h[6] += temp; queue_a_plop(6, temp);
- }
- return OVER;
- }
- else if (h[7]==0 && h[8]==0 && h[9]==0 && h[10]==0 && h[11]==0 && h[12]==0)
- {
- for (i=0; i<6; i++)
- {
- temp = h[i];
- h[i] = 0; queue_a_plop(i, -temp);
- h[6] += temp; queue_a_plop(6, temp);
- }
- return OVER;
- }
- else
- {
- if (p==6)
- return MY;
- else
- return YOUR;
- }
- }
-
-
-
- void translate_real_to_working(char * h)
- {
- int i;
- if (botgo)
- {
- for (i=0; i<6; i++)
- h[i] = bot[i];
- h[6] = bottot;
- for (i=7; i<13; i++)
- h[i] = top[i-7];
- h[13] = toptot;
- }
- else
- {
- for (i=0; i<6; i++)
- h[i] = top[i];
- h[6] = toptot;
- for (i=7; i<13; i++)
- h[i] = bot[i-7];
- h[13] = bottot;
- }
- }
-
-
-
-
-
-
-
- void real_move(int p)
- {
- /* now p can be 0...5 or 7...12 depending on whse go. */
- char h[14];
-
- if ( (p<0 || p>6) && botgo)
- werr(TRUE, "SNAFU at line %d", __LINE__);
- if ( (p<7 || p>12) && !botgo)
- werr(TRUE, "SNAFU at line %d", __LINE__);
- if (p<6)
- set_icon_select_state(main_window_handle, p, TRUE);
- else
- set_icon_select_state(main_window_handle, p, TRUE);
-
- translate_real_to_working(h);
- /* Now translated into display_move() format */
-
- switch ( display_move(h, (p>6) ? p-7 : p) )
- {
- case OVER:
- break;
-
- case MY:
- queue_a_plop(14+p, FALSE);
- break;
-
- case YOUR:
- queue_a_plop(14+p, TRUE);
- break;
- }
- }
-
-
-
-